﻿namespace TetrisProject.GameObjects
{
    using System;
    using System.Threading;
    using TetrisProject.Interfaces;

    public abstract class GameObject : IRenderable
    {
        protected MatrixCoords topLeft;
        protected bool[,] body;

        private static readonly MatrixCoords DOWN_VECTOR = new MatrixCoords(1, 0);
        private static readonly MatrixCoords LEFT_VECTOR = new MatrixCoords(0, -1);
        private static readonly MatrixCoords RIGHT_VECTOR = new MatrixCoords(0, 1);
        
        private static Random random = new Random();

        protected GameObject(int coordY, bool[,] body)
        {
            this.topLeft = new MatrixCoords(0, coordY);
            this.body = body;
        }
        
        public void MoveDown()
        {
            this.topLeft += GameObject.DOWN_VECTOR;
        }

        public void MoveLeft()
        {
            this.topLeft += GameObject.LEFT_VECTOR;
        }

        public void MoveRight()
        {
            this.topLeft += GameObject.RIGHT_VECTOR;
        }

        public virtual void Rotate()
        {
            int rows = this.body.GetLength(0);
            int cols = this.body.GetLength(1);

            var rotatedBody = new bool[cols, rows];

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    rotatedBody[j, i] = this.body[i, j];
                }
            }

            this.body = rotatedBody;
        }

        public MatrixCoords GetTopLeft()
        {
            return this.topLeft;
        }

        public bool[,] GetBody()
        {
            return this.body;
        }
    }
}
